package pe.com.csi.pm.regexiv;

import java.util.List;

import pe.com.csi.pm.regexiv.MyListAdapter.ViewHolder;
import pe.com.csi.pm.regexiv.bean.Alcantarilla;
import pe.com.csi.pm.regexiv.service.AlcantarillaService;
import pe.com.csi.pm.regexiv.util.Numero;


import android.app.Activity;
import android.content.Context;

import android.os.Bundle;
import android.view.View;
import android.view.Window;

import android.widget.ListView;
import android.widget.TextView;

public class ConsultarAlcantarilla extends Activity {

	private List<Alcantarilla> alcantarillas;
	private AlcantarillaService alcaService;
	private ListView lstOpciones;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		try {
			setContentView(R.layout.consultar_alcantarilla);
			alcaService = new AlcantarillaService(getApplicationContext());
			lstOpciones = (ListView) findViewById(R.id.lstAlcantarillas);
			cargarTabla();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void cargarTabla() {
		try {
			alcantarillas = alcaService.obtenerAll();
			lstOpciones.setAdapter(new AlcantarillaListAdapter(this, R.layout.item_alcantarilla, alcantarillas));

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	static class MyViewHolder extends ViewHolder {
		TextView txtPosition;
		TextView txtProyecto;
		TextView txtTramo;
		TextView txtUbicacion;
		TextView txtTipo;
		TextView txtSentido;

		public MyViewHolder(TextView txtPosition, TextView txtProyecto, TextView txtTramo, TextView txtUbicacion, TextView txtTipo, TextView txtSentido) {
			this.txtPosition = txtPosition;
			this.txtProyecto = txtProyecto;
			this.txtTramo = txtTramo;
			this.txtUbicacion = txtUbicacion;
			this.txtTipo = txtTipo;
			this.txtSentido = txtSentido;
		}

		public MyViewHolder() {

		}
	}

	private class AlcantarillaListAdapter extends MyListAdapter {

		public AlcantarillaListAdapter(Context context, int viewid, List<Alcantarilla> objects) {
			super(context, viewid, objects);
		}

		@Override
		protected void bindHolder(ViewHolder h) {
			try {
				MyViewHolder holder = (MyViewHolder) h;
				if (holder != null) {
					holder.txtPosition.setText("");
					holder.txtProyecto.setText("");
					holder.txtTramo.setText("");
					holder.txtUbicacion.setText("");
					holder.txtTipo.setText("");
					holder.txtSentido.setText("");
					Alcantarilla alca = (Alcantarilla) holder.data;
					if (alca != null) {
						holder.txtPosition.setText(alca.getPosition() + "");
						holder.txtProyecto.setText(alca.getProyecto());
						holder.txtTramo.setText(alca.getTramo());
						holder.txtUbicacion.setText(Numero.parseNumeroToString(alca.getUbicacion()));
						holder.txtTipo.setText(alca.getTipo());
						holder.txtSentido.setText(alca.getSentido());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		protected ViewHolder createHolder(View v) {
			TextView txtPosition = (TextView) v.findViewById(R.id.itemPosition);
			TextView txtProyecto = (TextView) v.findViewById(R.id.itemProyecto);
			TextView txtTramo = (TextView) v.findViewById(R.id.itemTramo);
			TextView txtUbicacion = (TextView) v.findViewById(R.id.itemUbicacion);
			TextView txtTipo = (TextView) v.findViewById(R.id.itemTipo);
			TextView txtSentido = (TextView) v.findViewById(R.id.itemSentido);

			ViewHolder holder = new MyViewHolder(txtPosition, txtProyecto, txtTramo, txtUbicacion, txtTipo, txtSentido);
			return holder;
		}
	}

}
